package com.projectnection.server.database.entities;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.projectnection.server.database.ObjectifyServiceCreation;
import com.projectnection.server.model.Entity;

public class ListEntityConnections {

	public static JSONArray jsonEntityConnections(String entityID) {

		return jsonListConnections(get(entityID));
	}

	public static Entity get(String entityID) {

		Entity entity = null;

		if (entityID != null) {

			Objectify objfyService = ObjectifyServiceCreation.gerService();

			entity = objfyService.get(Entity.class, entityID);
		}

		return entity;
	}

	public static JSONArray jsonListConnections(Entity entity) {

		JSONArray jsonEntityConnections = new JSONArray();

		for (Key<Entity> entityConnection : entity.getEntityConnections()) {

			jsonEntityConnections.put(jsonEntityConnection(entityConnection
					.getName()));
		}

		return jsonEntityConnections;
	}

	public static JSONObject jsonEntityConnection(String entityID) {

		JSONObject jsonEntity = null;

		try {

			jsonEntity = new JSONObject();

			jsonEntity.put("entityID", entityID);

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return jsonEntity;
	}
}